package ru.stepuk.modern.nodes.buffers;

import java.util.ArrayList;
import javax.swing.ImageIcon;
import ru.stepuk.modern.Model;
import ru.stepuk.modern.Request;
import ru.stepuk.modern.generators.RandomNumberGenerator;

/**
 *
 * @author ivan
 */
public class RandomAccessQueue extends Buffer {

    private RandomNumberGenerator rng;

    {
        icon = new ImageIcon(getClass().getResource("/images/b_random.png"));
    }

    public RandomAccessQueue(Model model) {
        super(model);
        this.rng = model.getGenerator();
        if (capacity > 0) {
            container = new ArrayList<Request>(capacity);
        } else {
            container = new ArrayList<Request>();
        }
    }

    @Override
    public void putRequest(Request request) {
        if (!hasFreeSpace()) {
            return;
        }
        container.add(request);
    }

    @Override
    public Request fetchRequest() {
        return container.get((int) Math.round(rng.nextValue() * container.size()));
    }

    @Override
    public void processRequest(Request request) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
